<?php
namespace app\tools\logic;
use think\facade\Db;

class ChannelServerLogic{
	static function getList(){
		$channel_id = input('channel_id', 0);
		$page_size = input('page_size', 10);
		$server_name = input('server_name', '');
		$obj = Db::name('channel_server')
				->alias('c')
				->field('c.*')
				->where('c.status','<>',127)
				->where('channel_id', $channel_id);
		$server_name && $obj->where('server_name|server_zh','like','%'.$server_name.'%');
		$list = $obj->order('c.sort asc,c.id desc')
				->paginate($page_size);
		return $list;
	}

	static function getInfo(){
		$id = input('id');
		if(!$id){
			return '';
		}
		$info = Db::name('channel_server')
				->alias('c')
				->field('c.*')
				->where('c.status','<>',127)
				->where('c.id', $id)
				->find();
		return $info;

	}

	static function save(){
		$id = input('id');
		if(!$id){
			return 110;
		}
		$data = [];
		$data['channel_id'] = input('channel_id','');
		if(!$data['channel_id']){
			return 125;
		}
		$info = Db::name('channel')->where('id', $data['channel_id'])->find();
		if(!$info){
			return 128;
		}
		$data['server_ico'] = input('server_ico', "");
		
		$data['server_name'] = input('server_name', '');
		$data['server_zh'] = input('server_zh','');
		$data['server_en'] = input('server_en','');
		$data['server_code'] = input('server_code', '');
		$base_rate = input('base_rate', 0);
		if(!$base_rate){
			$base_rate = $info['base_rate']?:3;
		}
		$data['base_rate'] = $base_rate;
		$data['sort'] = input('sort', 50);
		$data['status'] = input('status', 1);

		$gj = Db::name('channel_server')
				->field('id')
				->where('channel_id', $data['channel_id'])
				->where('server_code', $data['server_code'])
				->where('status','<>',127)
				->where('id','<>', $id)
				->find();
		if($gj){
			return 129;
		}

		$rs = Db::name('channel_server')->where('id', $id)->update($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	static function add(){
		$data = [];
		$data['channel_id'] = input('channel_id','');
		if(!$data['channel_id']){
			return 125;
		}

		$info = Db::name('channel')->where('id', $data['channel_id'])->find();
		if(!$info){
			return 128;
		}


		$data['server_ico'] = input('server_ico', "");
		$data['server_name'] = input('server_name', '');
		$data['server_zh'] = input('server_zh','');
		$data['server_en'] = input('server_en','');
		$data['server_code'] = input('server_code', '');
		$base_rate = input('base_rate', 0);
		if(!$base_rate){
			$base_rate = $info['base_rate']?:3;
		}
		$data['base_rate'] = $base_rate;
		$data['sort'] = input('sort', 50);
		$data['status'] = input('status', 1);
		$data['create_time'] = date('Y-m-d H:i:s');
		//服务是否已经存在
		$gj = Db::name('channel_server')
				->field('id')
				->where('channel_id', $data['channel_id'])
				->where('server_code', $data['server_code'])
				->where('status','<>',127)
				->find();
		if($gj){
			return 129;
		}

		$id = Db::name('channel_server')->insertGetId($data);
		if($id!==false){
			self::relateCountry($data['channel_id'], $id, $data['base_rate']);
			return 0;
		}else{
			return 100000;
		}
	}

	//删除
	static function delete(){
		$id = input('id','');
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}
		$rs = Db::name('channel_server')
				->where('id','in', $ids)
				->update(['status'=>127]);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	static function addList(){
		$channel_id = input('channel_id', 0);
		if(!$channel_id){
			return ['code'=>125];
		}
		$info = Db::name('channel')->where('id', $channel_id)->find();
		if(!$info){
			return ['code'=>128];
		}
		//通道修改为启用状态
		Db::name('channel')->where('id', $channel_id)->update(['status'=>1]);

		$item = input('item',[]);
		if(!$item){
			return ['code'=>126];
		}

		$cf = 0;
		$cg = 0;
		//批量新增
		foreach($item as $k=>$v){
			$v = trim($v,'-');
			$acc = array_values(array_filter(explode('@@', $v)));
			$data = [];
			$data['channel_id'] = $channel_id;
			$data['server_ico'] = $acc[0];
			
			//缺少国家代码
			if(isset($acc[1])){
				$data['server_code'] = $acc[1];
			}else{
				$cf++;
				continue;
			}
			//缺少国家名称
			if(isset($acc[2])){
				$data['server_name'] = $acc[2];
			}else{
				$cf++;
				continue;
			}
			
			if(isset($acc[3])){
				$data['server_zh'] = $acc[3];
			}

			if(isset($acc[4])){
				$data['server_en'] = $acc[4];
			}

			if(isset($acc[5])){
				$data['base_rate'] = $acc[5];
			}else{
				//默认直接给两倍
				$data['base_rate'] = $info['base_rate']?:3;
			}
			
			$data['status'] = 1;
			$data['sort'] = 50;
			$data['create_time'] = date('Y-m-d H:i:s');
			$info = Db::name('channel_server')
					->where('channel_id', $channel_id)
					->where('server_code', $data['server_code'])
					->find();

			if($info){
				Db::name('channel_server')->where('id', $info['id'])->update($data);
			}else{
				$id = Db::name('channel_server')->insertGetId($data);
				self::relateCountry($channel_id, $id, $data['base_rate']);
			}
			$cg++;
		}
		return ['code'=>0,'cf'=>$cf,'cg'=>$cg];
		
	}


	//关联本通道的所有服务
	static function relateCountry($channel_id=0, $server_id=0, $base_rate=0){
		if(!$channel_id || !$server_id){
			return;
		}

		$serverList = Db::name('channel_country')
						->field('id,base_rate,status')
						->where('channel_id', $channel_id)
						->where('status', 1)
						->select()->toArray();
		if($serverList){
			foreach($serverList as $k=>$v){
				$data = [];
				$data['channel_id'] = $channel_id;
				$data['channel_country_id'] = $v['id'];
				$data['channel_server_id'] = $server_id;
				$data['base_rate'] = $base_rate?:$v['base_rate'];
				Db::name('channel_country_server')->insert($data);
			}
		}
	}
}